﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Talentnet.Core.Database;
using Talentnet.Data.Entity;
using System.Data;
using System.Collections;
using Talentnet.Core.Utilities.Cryptophy;
using Talentnet.Core.Utilities;

namespace Talentnet.Data.Access
{
    partial class LoginUserDAL
    {
        public enum PasswordType
        {
            SysPwd = 1,
            UserPwd = 2
        }

        public List<LoginUser> GetList()
        {
            return LoginUser.CreateEntities(_dbProvider.ExecDataTable("sp_GetUserList"));
        }

        public SEC_User GetUser(int clientID, string loginName)
        {
            return DatabaseManager.TalentnetDB.SEC_Users.SingleOrDefault(u => u.ClientID == clientID && u.LoginName == loginName);
        }

        public List<SEC_User> GetUserList(int clientID, int roleID)
        {
            IEnumerable userList = (from users in DatabaseManager.TalentnetDB.SEC_Users
                                    where users.ClientID == clientID &&
                                    users.SEC_UsersInRoles.SingleOrDefault(r => r.RoleId == roleID) != null
                                    select users);
            return userList.Cast<SEC_User>().ToList();
        }

        public void DeleteUsersInRoles(int clientID, string loginName)
        {
            IEnumerable<SEC_UsersInRole> roles = (from r in DatabaseManager.TalentnetDB.SEC_UsersInRoles
                                                  where r.SEC_User.ClientID == clientID && r.SEC_User.LoginName == loginName
                                                  select r).ToList();
            DatabaseManager.TalentnetDB.SEC_UsersInRoles.DeleteAllOnSubmit(roles);
            DatabaseManager.TalentnetDB.SubmitChanges();
        }

        public void DeleteUser(int clientID, string loginName)
        {
            SEC_User user = GetUser(clientID, loginName);
            if (user != null)
            {
                DatabaseManager.TalentnetDB.SEC_Users.DeleteOnSubmit(user);
                DatabaseManager.TalentnetDB.SubmitChanges();
            }
        }

        public void ResetPassword(int clientID, string loginName)
        {
            SEC_User user = GetUser(clientID, loginName);
            if (user != null)
            {
                user.UserPassword = CryptoAgent.Current.MD5(Common.GenPwd());
                user.UserPasswordType = (int)PasswordType.SysPwd;
                user.UserPasswordDTS = DateTime.Now;
                DatabaseManager.TalentnetDB.SubmitChanges();
            }
        }

        public void CreateNewUser(SEC_User user)
        {
            DatabaseManager.TalentnetDB.SEC_Users.InsertOnSubmit(user);
            DatabaseManager.TalentnetDB.SubmitChanges();
        }

        public void UpdateUser(SEC_User user)
        {
            SEC_User userFromDB = DatabaseManager.TalentnetDB.SEC_Users.SingleOrDefault(u => u.LoginName == user.LoginName);
            if (user != null)
            {
                userFromDB.LoginName = user.LoginName;
                userFromDB.UserNameFirst = user.UserNameFirst;
                userFromDB.UserNameLast = user.UserNameLast;
                userFromDB.Email = user.Email;
                DatabaseManager.TalentnetDB.SubmitChanges();
            }
        }

        public void AssignRoleForUser(SEC_UsersInRole userInRole)
        {
            DatabaseManager.TalentnetDB.SEC_UsersInRoles.InsertOnSubmit(userInRole);
            DatabaseManager.TalentnetDB.SubmitChanges();
        }
    }

}
